﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using BySoft.Server.Common.WebCommon;
using BySoft.Server.Common.Enum;
using OneSoft.DataAccess;
using BySoft.Server.Consts;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Schema;
using BySoft.Server.DataAccess;
namespace BySoft.Server.Business
{
    /// <summary>
    /// SysParameter
    /// </summary>
    public partial class SysParameter
    {
        #region 缓存刷新
        /// <summary>
        ///  参数的缓存刷新
        /// </summary>
        public static void RefreshWebCaching()
        {

            DataSet ds = SysParameterDA.GetAll();
            WebCaching.ALLPARAMETERSCaching = ds;//缓存更新
        }
        #endregion

    
        #region isCodeExist
        /// <summary>
        ///   判断参数代号是否存在指定参数类别中
        /// </summary>
        /// <param name="strTypeID">参数类别</param>
        /// <param name="strCode">参数代码</param>
        /// <returns></returns>
        public static bool isCodeExist(string strTypeID, string strCode)
        {
            bool result = false;
            List<SysParameterEntity> list = SysParameterDA.GetByCondition("where upper(" + SysParameterTable.C_PARAMETERCODE + ")='" + strCode.Replace("'", "''").ToUpper() +
                "' and " + SysParameterTable.C_PARAMETERTYPEID + "='" + strTypeID.Replace("'", "''") + "' and " +
                SysParameterTable.C_STATUS + "<>'" + ((int)BaseEnum.CommonSatus.Delete).ToString() + "'");
            if (list != null && list.Count > 0 )
            {
                result = true;
            }
            return result;
        }
        #endregion

        #region isNameExist
        /// <summary>
        ///   判断参数名称是否存在指定参数类别中
        /// </summary>
        /// <param name="strTypeID">参数类别</param>
        /// <param name="strCode">参数名称</param>
        /// <returns></returns>
        public static bool isNameExist(string strTypeID, string strName)
        {
            bool result = false;
            string strWhere = " where ParameterName = '" + strName + "'" + " and ParameterTypeID ='" + strTypeID + "'" + " and Status <> '" + ((int)BaseEnum.CommonSatus.Delete).ToString() + "'";
            List<SysParameterEntity> list = SysParameterDA.GetByCondition( strWhere);
            if (list != null && list.Count > 0)
            {
                result = true;
            }
            return result;
        }
        #endregion

        #region GetParameterName

        /// <summary>
        ///   根据参数类别、参数代号获取参数名称
        /// </summary>
        /// <param name="strTypeID">类别ID</param>
        /// <param name="strParameterCode">参数代码</param>
        /// <returns></returns>
        public static String GetParameterName(string strTypeID, string strParameterCode)
        {
            String strResult = null;
            List<SysParameterEntity> lstParam = SysParameterDA.GetByCondition(" WHERE " + SysParameterTable.C_PARAMETERTYPEID + "='" + strTypeID + "' and " +
                              SysParameterTable.C_PARAMETERCODE + "='" + strParameterCode.Replace("'", "''") + "'");
            if (lstParam.Count > 0)
            {
                strResult = lstParam[0].ParameterName;
            }
            return strResult;

        }
        /// <summary>
        /// 根据参数类别、参数名称获取参数代号
        /// </summary>
        /// <param name="strTypeID">类别ID</param>
        /// <param name="strParameterName">参数名称</param>
        /// <returns></returns>
        public static String GetParameterCode(string strTypeID, string strParameterName)
        {
            String strResult = null;
            List<SysParameterEntity> lstParam = SysParameterDA.GetByCondition(" WHERE " + SysParameterTable.C_PARAMETERTYPEID + "='" + strTypeID + "' and " +
                              SysParameterTable.C_PARAMETERNAME + "='" + strParameterName.Replace("'", "''") + "'");
            if (lstParam.Count > 0)
            {
                strResult = lstParam[0].ParameterCode;
            }
            return strResult;

        }

        #endregion

        #region Insert
        public static bool Insert(SysParameterEntity entity)
        {
            if (entity == null)
                return false;
            //Changed by Alex,20090728,加入生成菜单功能
            bool bReturn = true;

            if (DataAccessCommon.Insert(entity,true))
            {
                RefreshWebCaching();
               
            }
            else
            {
                bReturn = false;
            }

            return bReturn;
        }
        #endregion

        #region 更新
        /// <summary>
        ///  更新
        /// </summary>
        /// <param name="entity">参数实体</param>
        /// <returns>bool 值</returns>
        public static bool Update(SysParameterEntity entity)
        {
            if (SysParameterDA.Update(entity) > 0)
            {
                RefreshWebCaching();//刷新缓存
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 逻辑删除
        /// <summary>
        ///  根据ID删除数据,逻辑删除
        /// </summary>
        /// <param name="strID">参数类别ID</param>
        /// <returns>布尔值</returns>
        public static bool Delete(SysParameterEntity entity)
        {
            Boolean result = true;

            if (SysParameterDA.UpdateStatus((int)entity.ParameterID, BaseEnum.CommonSatus.Delete) > 0)
            {
                RefreshWebCaching();

            }
            else
            {
                result = false;
            }
            return result;
        }
        #endregion

        public static DataSet GetParamAndParamType(DateTime lastDownLoad)
        {
            return SysParameterDA.GetParamAndParamType(lastDownLoad);
        }


        #region 根据PARAMTYPEID获得对应的parm
        public static DataSet GetParamsByTypeID(string paramTypeID)
        {
            return SysParameterDA.GetParamsByTypeID(paramTypeID);
        }
        #endregion

        //初始化技改的第一列
        public static DataTable GetEquipmentCatogries()
        {
            string strSql = "select spt.ParameterTypeID as Category, sp.ParameterCode as CategoryDetail,spt.ParameterTypeName as CategoryName,sp.ParameterName as CategoryDetailName,0 as LastMonthTMCumuluative";
            strSql+=" from SYS_Parameter sp,SYS_ParameterType spt ";
            strSql += " where spt.ParameterTypeID=sp.ParameterTypeID ";
            strSql += " and (spt.ParameterTypeID='FundCategory' or spt.ParameterTypeID='PropertyCategory') order by spt.ParameterTypeID";
            return DataAccessCommon.SelectDataSet(strSql).Tables[0];
        }
    }
}
